//
//  HomeViewController.m
//  GameBaby
//
//  Created by keyZhang on 13-12-19.
//  Copyright (c) 2013年 keyZhang. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableViewCell.h"
#import "DataService.h"
#import "HomeCellModel.h"
#import "QiandaoViewController.h"
#import "SearchViewController.h"


@interface HomeViewController ()

@end

@implementation HomeViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UILabel *t = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
        t.font = [UIFont systemFontOfSize:16];
        t.textColor = [UIColor blackColor];
        t.backgroundColor = [UIColor clearColor];
        t.textAlignment = UITextAlignmentCenter;
        t.text = @"E蜗卡";
        self.navigationItem.titleView = t;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    __block HomeViewController *this = self;
    self.homeTBview.finishBlock = ^{
        [this _loadMoreData];
    };
    self.homeTBview.rowHeight = 80.0;
    _dataList = [NSMutableArray array];
    self.homeTBview.refreshDelegate = self;
    [self initNavigationItem];
    [self loadData];
    
}

- (void)_loadMoreData {
    _dataList = [NSMutableArray array];
    
    if (!self.homeTBview.tableHeaderView) {
        [self.homeTBview initHeaderView];
    }
    
    [self loadData];
}


- (void)loadData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://m.a.ewoka.com/init/index/%d",self.dataList.count/10 +1];
    __weak HomeViewController *this = self;
    [DataService requestWithURL:urlStr
                         params:nil
                     httpMethod:@"GET"
                    finishBlock:^(id result) {
                        [this loadDataFinished:result];
                    }];
}


- (void)loadDataFinished:(NSDictionary *)result
{
    NSArray *array = [result objectForKey:@"game"];
    for (NSDictionary *dic in array) {
        HomeCellModel *model = [[HomeCellModel alloc] initContentWith:dic];
        [self.dataList addObject:model];
    }
    self.homeTBview.dataList = self.dataList;
    [self.homeTBview reloadData];
    [self.homeTBview doneLoadingTableViewData];
    if (self.dataList.count > 99) {
        self.homeTBview.isMore = NO;
    }else
    {
        self.homeTBview.isMore = YES;
    }
}

- (void)initNavigationItem
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"ico_search_nor.png"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"ico_search_sel.png"] forState:UIControlStateHighlighted];
    [button addTarget:self action:@selector(rightBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 40, 30);
    //创建导航按钮
    
    /**
     *  首页左上角签到按钮，下一版本添加
     */
//    UIButton *leftbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//    [leftbutton setImage:[UIImage imageNamed:@"64.png"] forState:UIControlStateNormal];
//    [leftbutton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
//    leftbutton.frame = CGRectMake(0, 0, 40, 30);

    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
//    UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:leftbutton];
    
//    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBarButtonClick:(UIButton *)btn
{
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

- (void)leftBarButtonClick:(UIButton *)btn
{
    QiandaoViewController *qdVC = [[QiandaoViewController alloc] init];
    [self.navigationController pushViewController:qdVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - BaseTableView delegate
//上拉加载的协议方法
- (void)pullUp:(BaseTableView *)tableView {
    NSString *str = [(UIButton *)[tableView valueForKey:@"moreButton"] titleLabel].text;
    if (![str isEqualToString:@"加载更多数据"]) {
        return;
    }else
    {
        [self loadData];
    }
//        [self loadData];
}


@end
